""""
Case Type   : D库常用语法兼容-常用函数兼容
Case Name   : 创建表查看INFORMATION_SCHEMA_TSQL.VIEWSCHECK_OPTION视图为none
Create At   : 2025-10-10
Owner       : chen-czywj
Description :
    1.使用兼容D库创建数据库
    2.创建表执行测试
    3.查看视图返回值正确无误
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.查看视图返回值正确无误
    4.清理环境
History     :
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node

log = Logger()
primary_sh = CommonSH('PrimaryDbUser')

class Function_Commonviews_Case0012(unittest.TestCase):
    def setUp(self):
        log.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.dbname_d = Node('PrimaryDbDmode').db_name
        self.dbname = 'test_views_001'
        self.schema_name = 'sys_view_test_01'
        self.table_name = 'employees'
        self.view_name = 'high_salary_employees'

    def test_Function_Commonviews(self):
        text = '-------------step1: 使用兼容D库创建数据库; expect: 成功-------------'
        log.info(text)
        msg = primary_sh.check_connection_status(self.constant.START_STATUS_MSG)
        self.assertTrue(msg)
        create_db = f'''drop database if exists {self.dbname};
            create database if not exists {self.dbname} dbcompatibility = 'D';'''
        res = primary_sh.execut_db_sql(create_db, dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, res)
        load_extension = primary_sh.execut_db_sql('create extension shark;',
                                     dbname=self.dbname)
        log.info(load_extension)
        self.assertIn(self.constant.create_extension_success, load_extension)

        text = '-------------step2: 创建表执行测试; expect: 成功-------------'
        log.info(text)
        create_table = f'''create schema {self.schema_name};
                set search_path to {self.schema_name};
                CREATE TABLE {self.table_name} (
                 name VARCHAR(50) NOT NULL,
                 age INT,
                 salary DECIMAL(10, 2),
                 CONSTRAINT chk_age CHECK (age >= 18)
                );
                CREATE VIEW {self.view_name} AS
                SELECT name, age, salary
                FROM employees
                WHERE age >= 18 AND salary > 5000;
                INSERT INTO employees (name, age, salary) VALUES ('张三', 25, 8000.00); 
                INSERT INTO employees (name, age, salary) VALUES ('李四', 26, 4000.00); 
                '''
        res = primary_sh.execut_db_sql(create_table, dbname=self.dbname)
        log.info(res)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG, res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res)
        self.assertIn(self.constant.CREATE_VIEW_SUCCESS_MSG, res)
        self.assertEqual(res.count('INSERT 0 1'), 2)

        text = '-------------step3: 查看视图返回值正确无误; expect: 返回值正确-------------'
        log.info(text)
        select_sql = f'''
            select table_schema, table_name, view_definition, check_option, is_updatable
            from information_schema_tsql.views v
            inner join pg_namespace s on v.table_schema = s.nspname
            where s.nspname = '{self.schema_name}';'''
        res = primary_sh.execut_db_sql(select_sql, dbname=self.dbname)
        log.info(res)
        self.assertIn('(1 row)', res)
        self.assertIn(self.view_name, res)
        self.assertIn('NONE', res)
        self.assertIn('YES', res)

    def tearDown(self):
        text = '------------------step4: 清理环境; expect: 成功------------------'
        log.info(text)
        drop_table = f'''set search_path to {self.schema_name};
            drop table if exists {self.table_name};
            drop view if exists {self.view_name};'''
        res = primary_sh.execut_db_sql(drop_table, dbname=self.dbname)
        log.info(res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res)
        drop_db = f'''drop database if exists {self.dbname};'''
        res = primary_sh.execut_db_sql(drop_db, dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, res)
